#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode() : val(0), left(nullptr), right(nullptr) {}
	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
	TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
/**
 * 使用队列迭代法
 */
class Solution{
public:
	vector<vector<int>> levelOrder(TreeNode* root){
		queue<TreeNode*> q;
		vector<vector<int>> result;
		if(root != nullptr) q.push(root);
		while(!q.empty()){
			int size = q.size();
			vector<int> levelNode;
			while(size--){
				TreeNode* node = q.front();q.pop();
				levelNode.push_back(node->val);
				if(node->left) q.push(node->left);
				if(node->right) q.push(node->right);
			}
			result.push_back(levelNode);
		}
		return result;
	}
};
/**
 * 使用递归法
 */
class Solution2{
private:
	void level(TreeNode* node,vector<vector<int>>& result,int depth){
		if(node == nullptr) return;
		if(result.size() == depth) result.push_back(vector<int>());
		result[depth].push_back(node->val);
		level(node->left,result,depth+1);
		level(node->right,result,depth+1);
	}
public:
	vector<vector<int>> levelOrder(TreeNode* root){
		int depth = 0;
		vector<vector<int>> result;
		level(root,result,depth);
		return result;
	}
};
int main(){
	
	return 0;
}
